import Net from './_Net';
import { AxiosRequestConfig } from 'axios';
import systemStore from '../store/SystemStore';
import webIM from './WebIM';
import message from 'antd/es/message'
import IMSDK from './IMSDK';

export default class NetService {

    private static _intance: NetService;

    private _net: Net;

    public constructor(_prk: string, _puk: string, _apikey: string, _appk: string) {
        this._net = Net.getInstance(_prk, _puk, _apikey, _appk);
    }

    get net(){
        return this._net
    }

    public static getInstance(_prk: string, _puk: string, _apikey: string, _appk: string): NetService {
        // this._intance = this._intance || new NetService(_prk, _puk, _apikey, _appk);
        this._intance = new NetService(_prk, _puk, _apikey, _appk);
        return this._intance
    }
    // public static setNull(){
    //     console.log('地址改变了吗',SystemStore.apiUrl)
    //     this.constructor();
    // }

    public login(url: any, data: any, config: AxiosRequestConfig): Promise<any> {
        return this.commonPost(url, data, config);
    }

    /**
     * 初始化Config数据
     */
    public initConfig(config: AxiosRequestConfig, url: string, data: any): Promise<any> {
        // if (url) {
        //     return this.commonGet(url, config)
        // } else {
        //     return this.request(config);
        // }
        // console.log('获取的值',url,data,config)
        return this.commonPost(url, data, config);
    }
    /**
     * 返回业务配置集
     * @returns 
     */
    public async initBizConfig(): Promise<any> {
        const path = '/user/chat/getAllSysDict'
        const data  = IMSDK._decorateData({}, path,'get')
        const ret = await this.commonGet(path, data);
        const systemDics = {}
        if(Array.isArray(ret.data)){
            ret.data.forEach(item=>{
                systemDics[item.dictType] = item.dictDataList
            })
        }
        return {
            dics:systemDics
        }
    }
 /**
     * 第二通道拉取消息
     */
    public TwoRestpost(config: AxiosRequestConfig, url: string, data: any): Promise<any> {
        // if (url) {
        //     return this.commonGet(url, config)
        // } else {
        //     return this.request(config);
        // }
        // console.log('获取的值',url,data,config)
        return this.commonPost(url, data, config);
    }

    public commonPost(url: any, data: any, config?: AxiosRequestConfig): Promise<any> {
        try {
            console.log('请求', url, data);
            const cfg = Object.assign({}, config || { headers: {} });
            if( !cfg.headers ) cfg.headers = {}
            const noTokenUrls = ['/user/login','/config']
            const noToken = noTokenUrls.find(item=>url.indexOf(item) != -1)
            if( noToken ){
                // 登录接口不能传token,否则xmpp 验证出问题
                delete cfg.headers['access_token']
            }else{
                if( !cfg.headers['access_token']){
                     if( systemStore.access_token){
                         cfg.headers.access_token = systemStore.access_token
                     }
                 }
            }
            let res = this._net.postRequest(url, data, cfg);
            res.then(result => {
                console.log('结果', url, result);
                if( result.resultCode ==0 ){
                    //message.error(result.data.resultMsg || '未知错误')
                    if( result.data && result.data.resultMsg ){
                        message.error(result.data.resultMsg || '未知错误')
                        return;
                    }
                }
                if (result.resultMsg && result.resultMsg == "访问令牌过期或无效") {
                    if (systemStore.access_token != '') {
                        message.warn('访问令牌过期或无效,请重新登录')
                    }
                    systemStore.access_token = '';
                    webIM.logout(true);
                }
            }).catch(err=>{
                message.error(err+'');
            })

            console.log('返回', url, res);
            return res
        } catch (e) {
            return new Promise(r => r(null))
        }
    }

    public commonGet(url: any, config?: AxiosRequestConfig | undefined): Promise<any> {
        return this._net.getRequest(url, config);
    }

    public request(config: AxiosRequestConfig): Promise<any> {
        return this._net.request(config);
    }
}
